package com.example.threaddemo.multithread;

import java.util.concurrent.CountDownLatch;

public class Worker implements Runnable
{
    private final CountDownLatch mStartSignal;
    private final CountDownLatch mDoneSignal;
    private final int mThreadIndex;

    Worker(final CountDownLatch startSignal, final CountDownLatch doneSignal,
           final int threadIndex)
    {
        this.mDoneSignal = doneSignal;
        this.mStartSignal = startSignal;
        this.mThreadIndex = threadIndex;
    }

    @Override
    public void run()
    {
        // TODO Auto-generated method stub
        try
        {
            mStartSignal.await();// 阻塞，等待mStartSignal计数为0运行后面的代码
            // 所有的工作线程都在等待同一个启动的命令
            doWork();// 具体操作
            System.out.println("Thread " + mThreadIndex + " Done Now:"
                    + System.currentTimeMillis());
            mDoneSignal.countDown();// 完成以后计数减一
        }
        catch (InterruptedException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void doWork()
    {
        for (int i = 0; i < 1000000; i++)
        {
            ;// 耗时操作
        }
        System.out.println("Thread " + mThreadIndex + ":do work");
    }
}

